package com.aoxon.cargo.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.Cloth;
import com.aoxon.cargo.bean.GsonBean;
import com.aoxon.cargo.cache.PurCache;
import com.aoxon.cargo.component.MyLoadCloth;
import com.aoxon.cargo.component.MyRadioButton;
import com.aoxon.cargo.component.n.WaterFallGoodsView;
import com.aoxon.cargo.service.ClickGoodsService;
import com.aoxon.cargo.service.MyServiceFactory;
import com.aoxon.cargo.service.SearchHotGoodsFromTextService;
import com.aoxon.cargo.service.SearchNewGoodsFromTextService;
import com.aoxon.cargo.util.CheckStateUtil;
import com.aoxon.cargo.util.DataUtil;
import com.aoxon.cargo.util.ExecutorUtil;
import com.google.gson.reflect.TypeToken;

public class PurGoodsSearchShowActivity extends Activity implements
		MyRadioButton.OnCheckedListener {

	private ImageView ivReturnButton;

	private TextView tvTitle;

	private WaterFallGoodsView wShowGoods;
	private MyRadioButton myRadioButton;

	private SearchHotGoodsFromTextService searchHotGoodsService = (SearchHotGoodsFromTextService) MyServiceFactory
			.getInstance(MyServiceFactory.SEARCH_HOT_GOODS);
	private SearchNewGoodsFromTextService searchNewGoodsService = (SearchNewGoodsFromTextService) MyServiceFactory
			.getInstance(MyServiceFactory.SEARCH_NEW_GOODS);
	private ClickGoodsService clickGoodsService = (ClickGoodsService) MyServiceFactory
			.getInstance(MyServiceFactory.CLICK_GOODS);

	private List<Cloth> cloths = new ArrayList<Cloth>();

	private String searchText;
	private int loadType = MyLoadCloth.SEARCH_NEW;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setup();

	}

	private void setup() {

		setContentView(R.layout.pur_search_goods_list);

		ivReturnButton = (ImageView) findViewById(R.id.ivPublicTitleReturn);
		tvTitle = (TextView) findViewById(R.id.tvPublicTiTle);

		myRadioButton = new MyRadioButton(this, "最新", "最热");
		myRadioButton.setOnCheckedChanged(this);

		wShowGoods = new WaterFallGoodsView(this, cloths);

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		searchText = bundle.getString("searchText");
		if (searchText != null) {
			tvTitle.setText(searchText);
		}
		wShowGoods.setToLoad(ToLoadNew);
		wShowGoods.loadData();

		ivReturnButton.setOnClickListener(myClickListener);
	}

	@Override
	public void checkOne() {
		// TODO Auto-generated method stub
		wShowGoods.clearView();
		wShowGoods.setToLoad(ToLoadNew);
		wShowGoods.loadData();
		loadType = MyLoadCloth.SEARCH_NEW;
	}

	@Override
	public void checkTwo() {
		// TODO Auto-generated method stub
		wShowGoods.clearView();
		wShowGoods.setToLoad(ToLoadHot);
		wShowGoods.loadData();
		loadType = MyLoadCloth.SEARCH_HOT;
	}

	WaterFallGoodsView.ToLoad ToLoadHot = new WaterFallGoodsView.ToLoad() {
		@Override
		public boolean doLoad() throws Exception {
			// TODO Auto-generated method stub
			GsonBean gsonBean = searchHotGoodsService.execute(
					wShowGoods.startIndex, wShowGoods.length, searchText);
			if (CheckStateUtil.check(gsonBean)) {
				List<Cloth> newCloths = DataUtil.gson.fromJson(
						gsonBean.getStrJson(), new TypeToken<List<Cloth>>() {
						}.getType());
				cloths.addAll(newCloths);
				wShowGoods.startIndex += newCloths.size();
				return true;
			} else {
				return false;
			}
		}
	};

	WaterFallGoodsView.ToLoad ToLoadNew = new WaterFallGoodsView.ToLoad() {
		public boolean doLoad() throws Exception {
			// TODO Auto-generated method stub
			Log.v("ToLoadNew", "start");
			GsonBean gsonBean = searchNewGoodsService.execute(
					wShowGoods.startIndex, wShowGoods.length, searchText);
			Log.v("ToLoadNew", gsonBean.toString());
			if (CheckStateUtil.check(gsonBean)) {
				List<Cloth> newCloths = DataUtil.gson.fromJson(
						gsonBean.getStrJson(), new TypeToken<List<Cloth>>() {
						}.getType());
				cloths.addAll(newCloths);
				wShowGoods.startIndex += newCloths.size();
				return true;
			} else {
				return false;
			}
		}
	};

	OnClickListener myClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.ivPublicTitleReturn:
				PurGoodsSearchShowActivity.this.finish();
				break;

			default:
				break;
			}
		}
	};

	OnItemClickListener onItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			PurCache.cloth = cloths.get(position);
			final int clothsId = cloths.get(position).getClothId();
			ExecutorUtil.getCachedExecutorService().submit(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					clickGoodsService.execute(clothsId);
				}
			});
			Intent intent = new Intent(PurGoodsSearchShowActivity.this,
					PurGoodsPagerActivity.class);
			MyLoadCloth.search_text = searchText;
			MyLoadCloth.setExtra(intent, wShowGoods.startIndex,
					wShowGoods.length, loadType, position,  cloths);
			startActivityForResult(intent, 1);
		}
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			this.finish();
			return false;
		}
		return false;
	}
}
